//
//  RootView.m
//  UI6_ScrollView_UIPageControl课后作业
//
//  Created by dllo on 15/11/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PhotoView.h"

@interface PhotoView ()<UIScrollViewDelegate>

//@property (nonatomic, retain) UIScrollView *scrollV;
@property (nonatomic, retain) UIPageControl *pageC;
@property (nonatomic, assign) NSInteger temp;

@end

@implementation PhotoView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.backgroundColor = [UIColor whiteColor];
    
    
    
    //UIScrollView
    self.scrollV = [[UIScrollView alloc] initWithFrame:self.frame];
    self.scrollV.contentSize = CGSizeMake(self.frame.size.width * 21, 0);
    self.scrollV.pagingEnabled = YES;
    self.scrollV.bounces = YES;
    self.scrollV.showsHorizontalScrollIndicator = YES;
    self.scrollV.contentOffset = CGPointMake(self.frame.size.width, 0);
    self.scrollV.delegate = self;
    
    [self addSubview:self.scrollV];
    [_scrollV release];
    
    
    //填图片
    
    
    for (NSInteger i = 0; i < 21; i++) {
        
        //每个页面的UIScrollView  专门缩放用
        UIScrollView *subScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(self.scrollV.frame.size.width * i, 0, self.scrollV.frame.size.width, self.scrollV.frame.size.height)];
        // 缩放大小
        // 缩放要签协议   (返回允许缩放的视图)
        subScrollV.delegate = self;
        subScrollV.minimumZoomScale = 0.5;
        subScrollV.maximumZoomScale = 10;
        [self.scrollV addSubview:subScrollV];
        [subScrollV release];
        
        NSString *fileName = nil;
        if (0 == i) {
            fileName = @"image19.jpg";
        } else if (20 == i) {
            fileName = @"image1.jpg";
        } else {
            fileName = [NSString stringWithFormat:@"image%ld.jpg", i];
        }
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, subScrollV.frame.size.width, subScrollV.frame.size.height)];
        imageV.image = [UIImage imageNamed:fileName];
        

        [subScrollV addSubview:imageV];
        [imageV release];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(170, 25, 30, 30);
        button.backgroundColor = [UIColor yellowColor];
        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [subScrollV addSubview:button];
    }
    
    
    // UIPageControl
    self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 550, 350, 100)];
    self.pageC.numberOfPages = 19;
    //    self.pageC.backgroundColor = [UIColor blackColor];
    self.pageC.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageC.currentPageIndicatorTintColor = [UIColor redColor];
    self.pageC.currentPage = 0;
    [self.pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    [self addSubview:self.pageC];
    [_pageC release];
    
    

    
}

- (void)buttonAction
{
    [self.delegate dismiss];
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    // 缩放时触发
    UIImageView *imageV = scrollView.subviews.firstObject;
    imageV.center = self.center;
    
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
    // 返回允许缩放的视图
//    return scrollView.subviews.firstObject;
    if (scrollView == self.scrollV) {
        return nil;
    }
    else
        
        return scrollView.subviews.firstObject;
 
}

- (void)pageAction:(UIPageControl *)sender
{
    //    页随点动
    self.scrollV.contentOffset = CGPointMake((self.pageC.currentPage + 1) * self.scrollV.frame.size.width, 0);
    
}

//// 减速将要开始
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
        // 放大时不好用
    
    if (scrollView == self.scrollV) {
        self.temp = self.pageC.currentPage;
        NSLog(@"减速开始");
    }
//    self.temp = self.pageC.currentPage;
//    NSLog(@"减速开始");
    
    
}

// 减速已经结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 点随页动
    if (scrollView == self.scrollV) {
        NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
        if (0 == index) {
            self.scrollV.contentOffset = CGPointMake(self.scrollV.frame.size.width * 19, 0);
            self.pageC.currentPage = 18;
        } else if (20 == index) {
            self.scrollV.contentOffset = CGPointMake(self.scrollV.frame.size.width * 1, 0);
            self.pageC.currentPage = 0;
        } else {
            self.pageC.currentPage = index - 1;
        }
        NSLog(@"减速结束");
    };
    
//    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
//    if (0 == index) {
//        self.scrollV.contentOffset = CGPointMake(self.scrollV.frame.size.width * 19, 0);
//        self.pageC.currentPage = 18;
//    } else if (20 == index) {
//        self.scrollV.contentOffset = CGPointMake(self.scrollV.frame.size.width * 1, 0);
//        self.pageC.currentPage = 0;
//    } else {
//        self.pageC.currentPage = index - 1;
//    }
//    NSLog(@"减速结束");
    
    
    // 返回原图大小
    
    //    // 放大时不好用
    if (self.temp != self.pageC.currentPage) {
        [[scrollView.subviews objectAtIndex:(self.temp + 1)] setZoomScale:1.0];
        //        self.temp = self.pageC.currentPage;
    }
//        if (self.temp != index) {
//            [[scrollView.subviews objectAtIndex:self.temp] setZoomScale:1.0];
//            self.temp = index;
//        }
    
    
}


@end
